//
//  XMHomeController.m
//  XiaoMiao
//
//  Created by 23 on 2018/1/19.
//  Copyright © 2018年 smallyou. All rights reserved.
//



//controller
#import "XMHomeController.h"
#import "SuccessToOriginController.h"
#import "XMNavigationController.h"
#import "XMMenuController.h"
#import "XMAvatorController.h"
#import "ConsumerParentController.h"
#import "ConsumerTypeSeletedController.h"
#import "LoginController.h"
#import "ConsumerListController.h"
#import "KBaseWebViewController.h"

//view
#import "ZYBannerView.h"

//model
#import "LoginInfoModel.h"

//request

@interface XMHomeController ()<ZYBannerViewDelegate,ZYBannerViewDataSource>
//底部背景view
@property (weak, nonatomic) IBOutlet UIView *bottomView;
//拍照
@property (weak, nonatomic) IBOutlet UIButton *photoButton;
//查询
@property (weak, nonatomic) IBOutlet XMHomeBottomButton *searchButton;
//我的
@property (weak, nonatomic) IBOutlet XMHomeBottomButton *mineButton;
//轮播图数组
@property (strong, nonatomic) NSArray * bannerArr;
@property (strong, nonatomic) ZYBannerView *bannerView;

@end

@implementation XMHomeController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
   [self.navigationController setNavigationBarHidden:YES animated:animated];
    [UIApplication sharedApplication].statusBarHidden = YES; 
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [UIApplication sharedApplication].statusBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;

    NSArray *btns = @[self.photoButton, self.searchButton, self.mineButton];
    for (UIButton *btn in btns) {
        [btn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    [self setupLoginShow];
    
    [AppNotificationHelpper addObserver:self selector:@selector(loginExpireNotification) name:kLoginExpireNotification object:nil];
    [AppNotificationHelpper addObserver:self selector:@selector(logoutNotification) name:kLogoutNotification object:nil];
    [self getBannerList];
}


#pragma mark ----netWork

-(void)getBannerList{
    MJWeakSelf
    [AppNetwork getHomeBannerCompletion:^(BOOL success, NSArray *list, NSString *errorMsg) {
        
        if (success) {
            weakSelf.bannerArr = list;
            [weakSelf.bannerView reloadData];
        }else{
            
        }
        
    }];
}

#pragma mark ----banner---


// 返回Banner需要显示Item(View)的个数
- (NSInteger)numberOfItemsInBanner:(ZYBannerView *)banner
{
    return self.bannerArr.count;
}

// 返回Banner在不同的index所要显示的View
- (UIView *)banner:(ZYBannerView *)banner viewForItemAtIndex:(NSInteger)index
{
    XMHomeBannerModel * model = self.bannerArr[index];
    UIImageView *imageView = [[UIImageView alloc] init];
    [imageView sd_setImageWithURL:[NSURL URLWithString:model.imageUrl] placeholderImage:nil];
    return imageView;
}

-(void)banner:(ZYBannerView *)banner didSelectItemAtIndex:(NSInteger)index{
    XMHomeBannerModel * model = self.bannerArr[index];
    if (model.url.length>0) {
        KBaseWebViewController * web = [[KBaseWebViewController alloc]init];
       
        [web.webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:model.url]]];
        [web.webView reload];
        [self.navigationController pushViewController:web animated:YES];
        
    }
    
}

#pragma mark - UI
- (void)setupLoginShow {
    if ([AppGlobal isLogin]) {
        return;
    }
    
    WEAKSELF
    LoginController *login = [[LoginController alloc] init];
    [login setLoginSuccessBlock:^(LoginInfoModel *loginInfoModel) {
        [weakSelf.navigationController dismissViewControllerAnimated:YES completion:nil];

    }];
    
    XMNavigationController *loginNav = [[XMNavigationController alloc] initWithRootViewController:login];
    [self presentViewController:loginNav animated:NO completion:nil];
}

#pragma mark - action
- (void)btnDidClick:(UIButton *)sender {
    
    if (sender == self.photoButton) {
        ConsumerTypeSeletedController *selete = [[ConsumerTypeSeletedController alloc] init];
        [self.navigationController pushViewController:selete animated:YES];
        
    }else if (sender == self.searchButton) {
        ConsumerListController *list1 = [[ConsumerListController alloc] initWithViewHeight:self.view.height isSearchType:YES vcType:ConsumerListVCTypeName];
        //        ConsumerParentController *parent = [[ConsumerParentController alloc] initWithVCType:ConsumerParentVCTypeSeletedOld];
        [self.navigationController pushViewController:list1 animated:YES];
//        ConsumerParentController *parent = [[ConsumerParentController alloc] initWithVCType:ConsumerParentVCTypeSearch];
//        [self.navigationController pushViewController:parent animated:YES];
//
    }else if (sender == self.mineButton) {
        //个人中心
        XMAvatorController *vc = [[XMAvatorController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}


//轮播图
-(ZYBannerView *)bannerView{
    if (!_bannerView) {
        _bannerView = [[ZYBannerView alloc] initWithFrame:CGRectZero];
        _bannerView.delegate = self;
        _bannerView.dataSource = self;
        _bannerView.autoScroll = YES;
        _bannerView.shouldLoop = YES;
        _bannerView.pageControl.currentPageIndicatorTintColor = kMainBlueColor;
        _bannerView.pageControl.pageIndicatorTintColor = kWhiteColor;
        [self.view addSubview:_bannerView];
        [self.view sendSubviewToBack:_bannerView];
        
        [_bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(self.view);
        }];
        
        
        //重设pagecontrol的位置
        [_bannerView setPageControlFrame:CGRectMake(0, 0, 0, 1)];
        
        [_bannerView.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_bannerView).offset(-158);
            make.centerX.equalTo(_bannerView);
            make.width.equalTo(_bannerView);
            make.height.equalTo(@32);

        }];
    }
    return _bannerView;
}


-(void)setBannerArr:(NSArray *)bannerArr{
    _bannerArr = bannerArr;
    [self.bannerView reloadData];
}

#pragma mark - Noti
- (void)loginExpireNotification {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您的登录信息已过期，请重新登录" preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"去登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
       [self clearInfo2OriginForLogout];
    }]];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)logoutNotification {
    
    [KLProgressHUD showPromptHUDWithMessage:@"退出成功"];
    
    [self clearInfo2OriginForLogout];
}

- (void)clearInfo2OriginForLogout {
    [AppGlobal logout];
    
    [self.navigationController popToRootViewControllerAnimated:NO];
    
    [self setupLoginShow];
}

@end









@implementation XMHomeBottomButton

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.imageView.frame = CGRectMake(0, 0, self.app_width, self.app_width);
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    self.titleLabel.app_width = self.app_width;
    self.titleLabel.app_centerX = self.app_width * 0.5;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.app_bottom = self.app_height;
}

@end
